#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5+10;

struct node{
	int val,ch[2],fa,siz,cnt;
}t[MAXN];

int root,cnt,n;

void pushup(int x){t[x].siz = t[t[x].ch[0]].siz + t[t[x].ch[1]].siz + t[x].cnt;}

void Rotate(int x){
	int y = t[x].fa,z = t[y].fa,k = t[y].ch[1] == x;
	t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
	t[y].ch[k] = t[x].ch[k^1];t[t[x].ch[k^1]].fa = y;
	t[x].ch[k^1] = y;t[y].fa = x; pushup(y);pushup(x);
}

void Splay(int x,int f){
	while(t[x].fa != f){
		int y = t[x].fa,z = t[y].fa;
		if(z != f)
			(t[y].ch[0] == x) ^ (t[z].ch[0] == y) ? Rotate(x):Rotate(y);
		Rotate(x);
	}if(!f) root = x;
}

void insert(int x){
	int u = root,f = 0;
	while(t[u].val != x && u) f = u,u = t[u].ch[x > t[u].val];
	if(!u) {
		u=++cnt;
		t[u].val = x;t[u].cnt = 1;t[u].fa = f,t[u].siz = 1,t[u].ch[0] = t[u].ch[1] = 0;
		if(f) t[f].ch[x > t[f].val] = u;
	}else t[u].cnt++;
	Splay(u,0);
}

void Find(int x){
	int u = root; if(!u) return;
	while(t[u].val != x && t[u].ch[x > t[u].val]) u = t[u].ch[x > t[u].val];
	Splay(u,0);
}

int Next(int x,int f){
	Find(x); int u = root;
	if((t[u].val > x && f) || (t[u].val < x && !f)) return u;
	u = t[u].ch[f];
	while(t[u].ch[f^1]) u = t[u].ch[f^1];return u;
}

void Delete(int x){
	int fro = Next(x,0),nxt = Next(x,1);
	Splay(fro,0);Splay(nxt,fro); int del = t[nxt].ch[0];
	if(t[del].cnt > 1) t[del].cnt--,Splay(del,0);
	else t[nxt].ch[0] = 0;
}

int K_th(int k){
	int u = root; if(t[u].siz < k) return 0;
	while(1){
		int y = t[u].ch[0];
		if(t[y].siz >= k) u = y;
		else if(k > t[y].siz + t[u].cnt) k -= t[y].siz + t[u].cnt,u = t[u].ch[1];
		else return t[u].val;
	}
}

int main (){
	insert(-0x7fffffff);insert(0x7fffffff);
	scanf("%d",&n);
	while(n--){
		int op,x;scanf("%d %d",&op,&x);
		switch(op){
		case 1:
			insert(x);
			break;
		case 2:
			Delete(x);
			break;
		case 3:
			Find(x);
			printf("%d\n",t[t[root].ch[0]].siz);
			break;
		case 4:
			printf("%d\n",K_th(x+1));
			break;
		case 5:
			printf("%d\n",t[Next(x,0)].val);
			break;
		case 6:
			printf("%d\n",t[Next(x,1)].val);
			break;
		}
	}
	return 0;
}
